package com.priox.sigar.util;

import com.priox.sigar.domain.bean.CeldaInfo;
import com.priox.sigar.domain.bean.ExcelProperties;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * User: Matrix
 * Date: 24/01/13
 */
public class ExcelUtil {

    private static final Logger LOG = LoggerFactory.getLogger(ExcelUtil.class);

    public static List obtenerFilasValidas(File archivoOrigen, ExcelProperties properties) {
        List result = new ArrayList();
        try {
            FileInputStream myInput = new FileInputStream(archivoOrigen);

            POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);

            HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);

            HSSFSheet mySheet = myWorkBook.getSheetAt(0);

            Iterator rowIter = mySheet.rowIterator();

            while (rowIter.hasNext()) {
                HSSFRow myRow = (HSSFRow) rowIter.next();

                List celdas = new ArrayList();
                int totalCell = myRow.getLastCellNum();
                for (int colIdx = 0; colIdx < totalCell; colIdx++) {
                    HSSFCell myCell = myRow.getCell(colIdx);
                    if (properties.esValido(colIdx, myCell)) {
                        celdas.add(getDataStringCell(myCell, properties.getFormatIndex(colIdx)));

                    }
                }

                if (esFilaValida(celdas)) {
                    LOG.debug("Ingresando fila con total de celdas " + celdas.size());
                    result.add(celdas);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String getDataStringCell(Cell cell, CeldaInfo celdaInfo) {
        String dataCell = "";
        if (cell == null) return dataCell;
        if (cell.getCellType() != Cell.CELL_TYPE_BLANK) {
            if (cell.getCellType() == (Cell.CELL_TYPE_STRING)) {
                if (StringUtils.isNotBlank(celdaInfo.getFormato())) {
                    SimpleDateFormat destinoPattern = new SimpleDateFormat("dd/MM/yyyy");
                    SimpleDateFormat origenPattern = new SimpleDateFormat(celdaInfo.getFormato());
                    try {
                        Date fecha = origenPattern.parse(cell.getStringCellValue().trim());
                        dataCell = destinoPattern.format(fecha);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                } else {
                    dataCell = cell.getStringCellValue().trim();
                }

            } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                if (StringUtils.isBlank(celdaInfo.getFormato())) {
                    DecimalFormat pattern = new DecimalFormat("###############");
                    dataCell = pattern.format(cell.getNumericCellValue());
                } else {
                    dataCell = String.valueOf(cell.getNumericCellValue());
                }

            }
        }
        return dataCell;
    }

    private static boolean esFilaValida(List celdas) {

        for (Object celda : celdas) {
            String celdaValue = (String) celda;
            if (StringUtils.isNotBlank(celdaValue)) {
                return true;
            }
        }
        return false;
    }
}
